import { defineStore } from "pinia";

export default defineStore("appStore", {
  state: () => {
    return {
      city: '福州',
	  tabBar: null,						// 请求后端TabBar数据信息
	  searchRecords: [],				// 搜索记录
	  searchRecommends: [],				// 搜索推荐
	  selectAddress: undefined,			// 下单地址
	  isSelectAddress: false,			// 下单是否选了地址
    };
  },
  actions: {
  	setSearchRecords(content){
  		let idx = this.searchRecords.indexOf(content)
  		if(idx != -1) {
  			this.searchRecords.splice(idx, 1)
  		}
  		this.searchRecords.unshift(content)
  	},
  	getSearchRecommends() {
  		uni.$xl.apis.getConfig({
  			params: {
  				key: 'productSearchRecommend'
  			}
  		}).then(res => {
  			this.searchRecommends = res.productSearchRecommend ? res.productSearchRecommend.split(',') : []
  		})
  	}
  },
  unistorage: {
    key: "xingluo", // 缓存的键，默认为该 store 的 id，这里是 appStore,
    paths: ["city", "searchRecords"], // 需要缓存的路径，这里设置 city 和 unReadMsgNum 会被缓存
  }
});